package com.teach;

import java.io.IOException;
import java.util.Random;

/**
 * <pre>
 * - 运行时异常,非检查异常
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-04-09 11:35
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 **/
public class Demo04 {

  public static void main(String[] args) {

    test1();//在调用声明了非检查异常的方法时,不必须对非检查异常进行处理,也不会编译错误
    test2();

    try{
      test1();
    }catch (Exception ex){
      //非检查异常(运行时异常)也可以捕获处理
      System.out.println("处理异常");
    }

    test1();
  }

  public static void test1()throws RuntimeException {
    //程序内部可能发生IOException
    if(new Random().nextBoolean()){
      throw new RuntimeException();
    }
  }
  public static void test2() {
    //程序内部可能发生IOException
    if(new Random().nextBoolean()){
      throw new RuntimeException();//能抛出方法上未声明的非检查异常
    }
  }
}
